package com.qyl.deguru.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.qyl.deguru.common.PageModel;
import com.qyl.deguru.dao.BaseDao;
import com.qyl.deguru.dao.UserDao;
import com.qyl.deguru.dto.User;
import com.qyl.deguru.mapper.UserMapper;

public class UserDaoImpl extends BaseDao implements UserDao {
	/*
	 * 实现UserDao层方法
	 */
	@Override
	public User getUserByUserIdAndPwd(String userId, String pwd) {//根据用户名和密码获取用户
		SqlSession sqlSession = this.getSqlSession();//获取连接	
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);		
		User user = userMapper.getUserByUserIdAndPwd(userId,pwd);	
		this.closeSqlSession();//关闭连接
		
		return user;
	}
	
	/*
	 * 根据用户Id获取用户
	 */
	@Override
	public User getUserById(String userId) {
		
		SqlSession sqlSession = this.getSqlSession();
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		User user=userMapper.getUserById(Integer.parseInt(userId));
		this.closeSqlSession();
		
		return user;
	}
	
	/*
	 * 用户列表展示
	 */
	@Override
	public List<User> userList(User user) {

		SqlSession sqlSession = this.getSqlSession();
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		List<User> list= userMapper.userList(user);
		this.closeSqlSession();
		
		return list;
	}
	/*
	 * 用户更新
	 */
	@Override
	public int updateUser(User user) {
		SqlSession sqlSession =getSqlSession();
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		int count=userMapper.updateUser(user);
		sqlSession.commit();
		closeSqlSession();
		return count;
	}
	/*
	 * 通过Id删除用户信息
	 */
	@Override
	public int delUserById(int id) {
		SqlSession sqlSession =getSqlSession();
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		int count=userMapper.delUserById(id);
		sqlSession.commit();
		closeSqlSession();
		return count;
	}

	@Override
	public int addUser(User user) {
		SqlSession sqlSession=getSqlSession();
		UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
		int count = userMapper.addUser(user);
		sqlSession.commit();
		closeSqlSession();
		return count;
	}

	@Override
	public List<User> getUsers(User user, PageModel pageModel) {
		SqlSession sqlSession=getSqlSession();
		UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
		List<User> users=userMapper.getUsers(user,pageModel);
		sqlSession.commit();
		closeSqlSession();
		return users;
	}

	@Override
	public int getCountByUser(User user) {
		SqlSession sqlSession=getSqlSession();
		UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
		int count = userMapper.getCountByUser(user);
		sqlSession.commit();
		closeSqlSession();
		return count;
		
	}

	@Override
	public int delBatchUsers(int [] ids) {
		SqlSession sqlSession=getSqlSession();
		UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
		int count=userMapper.delBatchUsers(ids);
		sqlSession.commit();
		closeSqlSession();
		return count;
	}

	@Override
	public void registerUser(User user) {
		SqlSession sqlSession=getSqlSession();
		UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
		userMapper.registerUser(user);
		sqlSession.commit();
		closeSqlSession();
	}

	@Override
	public int setActiveStatus(String active_code) {
		SqlSession sqlSession=getSqlSession();
		UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
		int count=userMapper.setActiveStatus(active_code);
		sqlSession.commit();
		closeSqlSession();
		return count;
		
	}

/*	@Override
	public User getUser(int uid) {
		SqlSession sqlSession = this.getSqlSession();
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		User user=userMapper.getUserById(Integer.parseInt(userId));
		this.closeSqlSession();
		
		return user;
	}*/



}
